package b2.widget.fight
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.scene.*;
	import b2.scene.fight.*;
	import b2.util.*;
	import b2.widget.*;
	
	import com.greensock.TweenLite;
	import com.smartfoxserver.v2.entities.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.*;
	import flash.text.*;
	
	import org.flixel.*;

	public class BattleLeftPane extends Sprite
	{
		public var campTopText:TextField;
		public var campBottomText:TextField;
		public var goalBottomText:TextField;
		public var playerSlots:Object;
		private var remainEnemyContainer:Sprite;
		private var remainTimeBottomText:TextField;
		
		public function BattleLeftPane()
		{
			super();
			playerSlots = new Object();
			//背景图片
			var BattlePaneBgr:Class = ResourceHelper.getInstance().getClassByName("BattleLeftPane");
			var battlePaneBgr:Sprite = new BattlePaneBgr();
			//背景图片缓存加速
			battlePaneBgr.cacheAsBitmap = true;
			addChild(battlePaneBgr);
			//战役名称
			campTopText = new TextField();
			campTopText.selectable = false;
        	campTopText.autoSize = TextFieldAutoSize.CENTER;
			campTopText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFF00);
			campTopText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
//			campTopText.text = "繁华的商路";
			campTopText.x = 44;
			campTopText.y = 58;
			addChild(campTopText);
			campBottomText = new TextField();
			campBottomText.selectable = false;
        	campBottomText.autoSize = TextFieldAutoSize.CENTER;
			campBottomText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFFFF);
			campBottomText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
//			campBottomText.text = "第 1 / 3 关";
			campBottomText.x = 44;
			campBottomText.y = 80;
			addChild(campBottomText);
			//胜利条件
			var goalTopText:TextField = new TextField();
			goalTopText.selectable = false;
        	goalTopText.autoSize = TextFieldAutoSize.LEFT;
			goalTopText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFF00);
			goalTopText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			goalTopText.text = "胜利条件";
			goalTopText.x = 20;
			goalTopText.y = 124;
			addChild(goalTopText);
			goalBottomText = new TextField();
			goalBottomText.selectable = false;
        	goalBottomText.autoSize = TextFieldAutoSize.CENTER;
			goalBottomText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFFFF);
			goalBottomText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
//			goalBottomText.text = "全歼敌军";
			goalBottomText.x = 44;
			goalBottomText.y = 148;
			addChild(goalBottomText);
			//剩余时间
			var remainTimeTopText:TextField = new TextField();
			remainTimeTopText.selectable = false;
        	remainTimeTopText.autoSize = TextFieldAutoSize.LEFT;
			remainTimeTopText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFF00);
			remainTimeTopText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			remainTimeTopText.text = "剩余时间";
			remainTimeTopText.x = 20;
			remainTimeTopText.y = 190;
			addChild(remainTimeTopText);
			remainTimeBottomText = new TextField();
			remainTimeBottomText.selectable = false;
        	remainTimeBottomText.autoSize = TextFieldAutoSize.LEFT;
			remainTimeBottomText.defaultTextFormat = new TextFormat("微软雅黑", 20, 0xFFFFFF, true);
			remainTimeBottomText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
//			remainTimeBottomText.text = "3分1秒";
			remainTimeBottomText.x = 18;
			remainTimeBottomText.y = 217;
			addChild(remainTimeBottomText);
			//剩余坦克
			var remainTankTopText:TextField = new TextField();
			remainTankTopText.selectable = false;
        	remainTankTopText.autoSize = TextFieldAutoSize.LEFT;
			remainTankTopText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFF00);
			remainTankTopText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			remainTankTopText.text = "剩余敌军";
			remainTankTopText.x = 20;
			remainTankTopText.y = 270;
			addChild(remainTankTopText);
			//退出按钮
			var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr0");
			var quitButton:BasicButton = new BasicButton(ButtonBgr, "退出", 15, 75.6, 24.3);
			quitButton.x = 8;
			quitButton.y = 545;
			quitButton.addEventListener(MouseEvent.CLICK, onQuitButtonClick);
			addChild(quitButton);
		}
		
		public function onClockTick(event:FightEvent):void
		{
			if (FlxG.state != null)
			{
				var state:PlayState = FlxG.state as PlayState;
				var minutes:int = state.remainTime / 60;
				var seconds:int = state.remainTime % 60;
				var timeStr:String = (minutes < 10 ? "0" : "") + minutes + ":" + (seconds < 10 ? "0" : "") + seconds;
				remainTimeBottomText.text = timeStr;
			}
		}
		
		public function clearPlayerSlot():void
		{
			for (var tankId:String in playerSlots)
			{
				removePlaySlot(parseInt(tankId));
			}
			playerSlots = new Object();
		}
		
		public function removePlaySlot(tankId:int):void
		{
			if (playerSlots[tankId] != null)
			{
				removeChild(playerSlots[tankId]);
				delete playerSlots[tankId];
			}
		}
		
		public function loadAllPlayerSlots(campIndex:int, isHeroMode:Boolean = false):void
		{
			clearPlayerSlot();
			var campConfig:CampConfig = Config.campConfigs[campIndex];
			var playerSlot:PlayerSlot;
			var me:PlayerInfo = PlayerInfoManager.getInstance().me;
			//英雄模式
			if (isHeroMode)
				playerSlot = new PlayerSlot(me.id, me.nickname, 1, 5);
			//普通模式
			else
				playerSlot = new PlayerSlot(me.id, me.nickname, campConfig.lifeNum, 5);
			playerSlot.x = 8;
			playerSlot.y = 348;
			addChild(playerSlot);
			playerSlots[me.campTankId] = playerSlot;
		}
		
		private function refreshPlayerSlotOrder():void
		{
			var tankIds:Array = new Array();
			for (var tankId:String in playerSlots)
			{
				tankIds.push(tankId);
			}
			tankIds.sort(sortPlayerSlot);
			for (var i:int = 0; i < tankIds.length; i++)
			{
				var playerSlot:PlayerSlot = playerSlots[tankIds[i]];
				TweenLite.to(playerSlot, 1, {y:348 + i * 65});
				if (i < 3)
					playerSlot.visible = true;
				else
					playerSlot.visible = false; 
			}
		}
		
		private function sortPlayerSlot(a:int, b:int):int
		{
			var playerSlotA:PlayerSlot = playerSlots[a];
			var playerSlotB:PlayerSlot = playerSlots[b];
//			if (playerSlotA.playerId == PlayerInfoManager.getInstance().myUserId)
//				return -1;
//			else if (playerSlotB.playerId == PlayerInfoManager.getInstance().myUserId)
//				return 1;
			if (playerSlotA.killNum > playerSlotB.killNum)
				return -1;
			else if (playerSlotA.killNum < playerSlotB.killNum)
				return 1;
			else
				return 0;
		}
		
		//计算势力的所有生命条数
		public function getLifeTotal():int
		{
			var lifeTotal:int = 0;
			for (var tankIdStr:String in playerSlots)
			{
				var playerSlot:PlayerSlot = playerSlots[tankIdStr];
				lifeTotal += playerSlot.lifeNum;
			}
			return lifeTotal;
		}
		
		//更新剩余敌军数量
		public function updateRemainEnemy(enemyRemainNum:int):void
		{
			 if (remainEnemyContainer != null && remainEnemyContainer.parent != null)
			 	remainEnemyContainer.parent.removeChild(remainEnemyContainer);
			 remainEnemyContainer = new Sprite();
			 for (var i:int = 0; i < Math.min(enemyRemainNum, 30); i++)
			 {
				var TankLifeIcon:Class = ResourceHelper.getInstance().getClassByName("TankLifeIcon");
				var tankLifeIcon:Sprite = new TankLifeIcon();
				tankLifeIcon.width = 6;
				tankLifeIcon.scaleY = tankLifeIcon.scaleX;
				tankLifeIcon.x = (i % 10) * 7;
				tankLifeIcon.y = int(i / 10) * 13;
				tankLifeIcon.filters = [new ColorMatrixFilter([1,0,0,0,100,
			     0,1,0,0,100,
			     0,0,1,0,100,
			     0,0,0,1,0 ])];
				remainEnemyContainer.addChild(tankLifeIcon);
			 }
			 remainEnemyContainer.x = 11;
			 remainEnemyContainer.y = 290;
			 addChild(remainEnemyContainer);
		}
		
		public function updateTankInfo(tankId:int, lifeNum:int, killNum:int, syncFlag:Boolean = false):void
		{
			var playerSlot:PlayerSlot = playerSlots[tankId];
			if (playerSlot != null)
			{
				playerSlot.lifeNum = lifeNum;
				playerSlot.killNum = killNum;
				refreshPlayerSlotOrder();
			}
		}
		
		public function onTankLifePlus(event:FightEvent):void
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().me;
			if (event.targetTankId == me.campTankId)
			{
				var playerSlot:PlayerSlot = playerSlots[me.campTankId];
				updateTankInfo(me.campTankId, playerSlot.lifeNum + event.lifePlus, playerSlot.killNum, true);
			}
		}
		
		public function onTankKilled(event:FightEvent):void
		{
			//更新面板信息
			var me:PlayerInfo = PlayerInfoManager.getInstance().me;
			var playerSlot:PlayerSlot = playerSlots[me.campTankId];
			if (event.conductTankId == me.campTankId)
			{
				updateTankInfo(me.campTankId, playerSlot.lifeNum, playerSlot.killNum + 1, true);
			}
			if (event.targetTankId == me.campTankId)
			{
				updateTankInfo(me.campTankId, playerSlot.lifeNum - 1, playerSlot.killNum, true);
				if (playerSlot.lifeNum > 0)
				{
					var state:PlayState = FlxG.state as PlayState;
					state.createMyTank();
				} 
			}
		} 
		
		private function onQuitButtonClick(event:MouseEvent):void
		{
			var messageBox:MessageBox = new MessageBox("确定要退出战斗？", true, 0.8);
			messageBox.setOKCall(onQuitOKClick);
			WidgetManager.getInstance().popOut([messageBox], false, true);
		}
		
		private function onQuitOKClick():void
		{
			var goalFlag:int = -1;
			//同步服务器
			ServerHelper.getInstance().endFight(FightScene.getInstance().campIndex, goalFlag, FightScene.getInstance().generateMyAwardBalance());
			//退出游戏
			FightScene.getInstance().quit();
		}
	}
}